home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1453.dms / var1453.adf / Lists / Example3.c < prev    next >
C/C++ Source or Header  |  1992-05-02  |  3KB  |  105 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM System                  Amiga C Club       */
  7. /* Chapter: Lists                       Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* Demonstrates how to scan through a list looking for */
  21. /* nodes with a special name. Uses the function        */
  22. /* FindName().                                         */
  23.  
  24.  
  25. #include <exec/types.h>
  26. #include <exec/lists.h> /* This file will automatically */
  27.                         /* include the file "nodes.h".  */
  28.  
  29. /* Declare a complete node stucture: */
  30. struct ToDo
  31. {
  32.   struct Node node; /* Every node must have this. */
  33.   STRPTR Wish;      /* Our own data.              */
  34. };
  35.  
  36.  
  37. /* Declare a list structure: */
  38. struct List my_list;
  39.  
  40.  
  41. /* Node 1: */
  42. struct ToDo eat=
  43. {
  44.   { NULL, NULL, NT_UNKNOWN, 0, "Food" },
  45.   "eat breakfast"
  46. };
  47.  
  48. /* Node 2: */
  49. struct ToDo sleep=
  50. {
  51.   { NULL, NULL, NT_UNKNOWN, 0, "Sleep" },
  52.   "rest"
  53. };
  54.  
  55. /* Node 3: */
  56. struct ToDo drink=
  57. {
  58.   { NULL, NULL, NT_UNKNOWN, 0, "Food" },
  59.   "drink some nice wine"
  60. };
  61.  
  62.  
  63. main()
  64. {
  65.   struct Node *node_ptr; /* Node pointer. */
  66.   struct ToDo *ptr;       /* Pointer to ToDo structures. */
  67.  
  68.   
  69.   /* Initialize our list structure: */
  70.   NewList( &my_list );
  71.  
  72.  
  73.   /* Add three nodes: */
  74.   printf( "Adding some nodes...\n" );
  75.   AddTail( &my_list, &eat );
  76.   AddTail( &my_list, &sleep );
  77.   AddTail( &my_list, &drink );
  78.  
  79.  
  80.   /* Find the first node that is about Food: */
  81.   node_ptr = (struct Node *) FindName( &my_list, "Food" );
  82.   
  83.   /* As long as we find nodes about Food stay in the loop: */
  84.   while( node_ptr )
  85.   {
  86.     /* Copy the address of the node into the DoDo pointer: */
  87.     ptr = (struct ToDo *) node_ptr;
  88.  
  89.     /* Print the nodes wish: */
  90.     printf( "I want to %s.\n", ptr->Wish );
  91.  
  92.     /* Try to find yet another node about Food:  */
  93.     /* (Are you getting hungry?)                 */
  94.     node_ptr = (struct Node *) FindName( node_ptr, "Food" );
  95.   }
  96.  
  97.  
  98.   /* Remove all nodes from the list: */
  99.   printf( "Remove all nodes...\n" );
  100.   RemHead( &my_list, &eat );
  101.   RemHead( &my_list, &sleep );
  102.   RemHead( &my_list, &drink );
  103. }
  104.  
  105.